﻿$(function () {
    categories.CategoryProductMapping();
    categories.CategoryUI();
});
var token = $('input[name="__RequestVerificationToken"]').val();
var categoryId = $('#category-id').val();
var categories = {
    CategoryUI: function () {
        $('#category-part-edit').tabs();
        var mediaList = $(".media-list-edit").html();
        $(".media-list-edit").remove();
        if (categoryId != 0) {
            $(".category-media-list").html(mediaList);
        }
    },
    CategoryProductMapping: function () {
        $('#category-mapping').click(function () {
            $('.category-product-mapping').html("<img src='/Modules/Dyn.Ecommerce/Styles/images/loading.gif'/>")
                            .load("/Ecommerce/CategoryAdmin/LoadProductCategoryMapping?catId=" + categoryId);
        });
        $('#category-product-mapping a.edit').live("click", function () {
            var contentId = $(this).attr("href").replace('#', '');
            $("#product-category-update-" + contentId + "").show();
            $("#checkbox-featured-" + contentId + "").show();
            $("#text-featured-" + contentId + "").hide();
            $("#label-display-order-" + contentId + "").hide();
            $("#edit-display-order-" + contentId + "").show();
            $(this).hide();
        });
        $('#category-product-mapping a.update').live("click", function () {
            var contentId = $(this).attr("href").replace('#', '');
            $("#product-category-edit-" + contentId + "").show();
            $("#checkbox-featured-" + contentId + "").hide();
            $("#text-featured-" + contentId + "").show();
            $("#label-display-order-" + contentId + "").show();
            $("#edit-display-order-" + contentId + "").hide();
            $(this).hide();
            //Update content-product
            var isFeature = false;
            if ($("#checkbox-featured-" + contentId + "").is(':checked')) {
                isFeature = true;
            }
            var displayIndex = $("#edit-display-order-" + contentId + "").val();
            $.post('/Ecommerce/CategoryAdmin/UpdateProductCategoryMapping', { isFeature: isFeature, displayIndex: displayIndex, contentId: contentId, __RequestVerificationToken: token },
                      function (data) {
                          $("#text-featured-" + contentId + "").text(isFeature);
                          $("#label-display-order-" + contentId + "").text(displayIndex);
                      }
                    );
        });
        $('#category-product-mapping a.p_delete').live("click", function () {
            var contentId = $(this).attr("href").replace('#', '');
            $.post('/Ecommerce/CategoryAdmin/DeleteProductCategoryMapping', { contentId: contentId, __RequestVerificationToken: token },
                      function (data) {
                          $('#category-product-mapping tr#row-' + contentId + '').remove();
                      }
                    );
        });
        $('#add-prduct-category').live("click", function (event) {
            var url = $(this).attr("href");
            var windowName = $(this).attr("name");
            var windowSize = "width=700,height=800,scrollbars=yes";
            window.open(url, windowName, windowSize);
            event.preventDefault();
        });
    }
};


    